package frame;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.io.File;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.filechooser.FileFilter;

import function.fileinfo;
import function.filewrite;

public class filesave extends JDialog{
	public filesave(fileinfo fi,String url){
		setTitle("VW-Edit 保存");
		setBounds(0, 0, 600, 400);
		setLocationRelativeTo(null);
		setIconImage(Toolkit.getDefaultToolkit().getImage("./material/logo/fvw.png"));
		setModal(true);
		setMinimumSize(new Dimension(600, 400));
		
		JFileChooser fc=new JFileChooser();
		fc.setDialogType(JFileChooser.SAVE_DIALOG);
		for(int i=0;i<16;i++)fc.addChoosableFileFilter(getFileFilterOfNum(i));
		fc.setSelectedFile(new File(fi.getName()));
		fc.setMinimumSize(new Dimension(600, 400));
		if(url!=null)fc.setSelectedFile(new File(url));
		int a=fc.showSaveDialog(this);
		if(a==JFileChooser.APPROVE_OPTION) {
			try {
				String fn=fc.getSelectedFile().getName();
				if(!fi.getName().equals(fn))
					fi.setName(fn);
				String hz=fc.getFileFilter().getDescription();
				if(!hz.equals("所有文件")) {
					hz=hz.substring(hz.indexOf("*")+1, hz.length()-1);
					fi.setType(hz);
				}
				new filewrite().write(fi, fc.getCurrentDirectory().getCanonicalPath().replaceAll("\\\\", "/"));
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	private String filename[][]= {{".txt","文本文档(*.txt)"},{".java","JAVA源文件(*.java)"},{".cpp","C++源文件(*.cpp)"},{".h","C++源文件(*.h)"}
								,{".css","叠层样式表(*.css)"},{".xml","可扩展标记语言文件(*.xml)"},{".py","Python源文件(*.py)"},{".pyw","Python源文件(*.pyw)"}
								,{".bat","批处理文件(*.bat)"},{".cs","C#源码(*.cpp)"},{".json","JSON文件(*.json)"},{".js","JavaScript源文件(*.js)"}
								,{".vw","VW文件(*.vw)"},{".c","C源文件(*.c)"},{".jsp","JavaServer源文件(*.jsp)"},{".lua","Lua源文件(*.lua)"}};
	private FileFilter getFileFilterOfNum(int num) {
		return new FileFilter() {
			@Override
			public String getDescription() {
				return filename[num][1];
			}
			@Override
			public boolean accept(File f) {
				if(f.getName().endsWith(filename[num][0])||f.isDirectory())return true;
				return false;
			}
		};
	}
}
